// pages/my/integral/index.js
const {
  baseURL
} = require('../../../fetch/http')
const api = require('../../../fetch/api')
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    arr: [],
    loading: false,
    finished: false,
    page: 1,
    type: 0,
  },
  clear() {
    this.setData({
      page: 1,
      loading: false,
      finished: false,
      arr: [],
    })
  },
  async redLog() {
    let apiMethod = this.data.type == 0 ? "balanceLog" : "integralLog"
    if (!this.data.loading) {
      this.setData({
        loading: true
      })
      let res = await api[apiMethod]({
        page: this.data.page
      })
      let finished = false
      res.data.length == 0 && (finished = true)
      this.setData({
        arr: [...this.data.arr, ...res.data],
        finished,
        loading: false
      })
    }
  },
  mall() {
    wx.navigateTo({
      url: '/pages/my/mall/index',
    })
  },
  async balance() {
    let res = await api.redPacket()
    let {
      balance,
      todayBalance,
      totalBalance,
      integral,
      todayIntegral,
      totalIntegral
    } = res.data
    this.setData({
      balance,
      todayBalance,
      totalBalance,
      integral,
      todayIntegral,
      totalIntegral
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.type);
    this.setData({
      type: options.type
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.balance()
    this.clear()
    this.redLog()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})